<?php

namespace app\home\controller;

use app\common\model\mysql\Category;
use app\common\model\mysql\SpecValue;
use app\home\logic\GoodsLogic;

class Goods extends Base
{
    //分类下的商品列表
    public function index()
    {

        //$id是分类id
        //接受参数
        $keywords = input('keywords');
        $id = input('id');
        if(empty($keywords)){
            //查询分类下的商品
            //获取指定分类下的商品列表

            if(!preg_match('/^\d+$/',$id)){
                $this->error('参数错误');
            }
            $list = \app\common\model\mysql\Goods::where('cate_id',$id)->order('id','desc')->paginate([
                'list_rows' => 10,
                'query' => request()->param()

            ]);
            halt($list);

            //查询分类信息
            $cate_info = Category::find($id);
            $cateName = $cate_info['cate_name'];
            
        }else{
            try {
                //从es中搜索
                $list = GoodsLogic::search();


                $cateName = $keywords;
            }catch (\Exception $e){
                $this->error('服务器异常');
            }
        }


        //渲染模板
        return view('index',['list'=>$list,'cate_name' => $cateName]);
    }

    //获取商品详情
    public function detail($id)
    {
        //$id是商品id
        //查询商品信息,商品相册,规格商品sku
        $goods = \app\common\model\mysql\Goods::with(['goods_images','spec_goods'])->find($id);



        $goods['goods_attr'] = json_decode($goods['goods_attr'],true);//true表示转换成数组
//        halt(empty($goods['spec_goods']->toArray()));
        //查询商品的规格名称规格值 组装数组
        //$goods['spec_goods'] 二维数组 value_ids字段  想得到$value_ids = [28,29,30,32,33]
        if(!empty($goods['spec_goods']->toArray())){
            //将商品的第一个规格商品的信息 替换到$goods中
            if($goods['spec_goods'][0]['price']>0){
                $goods['goods_price'] = $goods['spec_goods'][0]['price'];

            }
            if($goods['spec_goods'][0]['cost_price']>0){
                $goods['cost_price'] = $goods['spec_goods'][0]['cost_price'];
            }
            if($goods['spec_goods'][0]['store_count']>0){
                $goods['store_count'] = $goods['spec_goods'][0]['store_count'];
            }else{
                $goods['store_count']  = 0;
            }




            $valueIds = array_unique(explode('_',implode('_',array_column($goods['spec_goods']->toArray(),'value_ids'))));
            //根据IDS[28,29,32,33] 查询spec_value表
            $spec_values = SpecValue::with('spec')->select($valueIds)->toArray();
//            $a = array_column($spec_values,null,'spec_id');
//            foreach ($spec_values as $value){
//                $a[$value['spec_id']]['spec_values'][] = $value;
//            }
//            halt($a);
            $res = [];
            foreach ($spec_values as $value)
            {
                $res[$value['spec_id']] = [
                    'spec_id' => $value['spec_id'],
                    'spec_name' => $value['spec_name'],
                    'spec_values' => []
                ];
            }
            foreach ($spec_values as $value)
            {
                $res[$value['spec_id']]['spec_values'][] = $value;
            }


        }else{
            $res = [];
        }

//       $value_ids_map =  ["18_39" => 811
//  "18_40" => 812
//  "19_39" => 813
//  "19_40" => 814
//]

        $value_ids_map = json_encode(array_column($goods['spec_goods']->toArray(),null,'value_ids'));
//        halt($value_ids_map);
//        halt(gettype($value_ids_map));
//        halt(json_encode(['12'=>312]));

        return view('detail',['goods'=>$goods,'specs' => $res,'value_ids_map'=>$value_ids_map]);

    }
}